package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {


    private static final Logger log = LoggerFactory.getLogger(ShoppingCartServiceImpl.class);
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
        //判断当前加入到购物车的商品是否存在
        ShoppingCart shoppingCart = new ShoppingCart();
        //重新封装
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //获取token里面的用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //List集合封装结果
        List<ShoppingCart> shippingCartList = shoppingCartMapper.select(shoppingCart);
        //如果已经存在，只需要数量加1
        if(shippingCartList != null&& !shippingCartList.isEmpty()){
            //获取第一条数据
           ShoppingCart  shoppingCart1 = shippingCartList.get(0);
           //数量+1
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
           shoppingCartMapper.updataNumber(shoppingCart1);

        }else {
            //如果不存在需要新加入一条购物车数据
            Long dishId = shoppingCart.getDishId();
            Long setmealId = shoppingCart.getSetmealId();

            if(dishId!=null){
                //传入的是菜品，根据菜品id查询菜品的数据，再封装到当前的shoppingCart中
                DishVO dishVO = dishMapper.selectDishById(dishId);
                shoppingCart.setName(dishVO.getName());
                shoppingCart.setImage(dishVO.getImage());
                shoppingCart.setAmount(dishVO.getPrice());

            }
            if(setmealId!=null){
                //传入的是套餐
                SetmealVO setmealVO = setmealMapper.selectById(setmealId);
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setAmount(setmealVO.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            log.info("购物车插入数据：{}", shoppingCart);
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    //展示该用户的购物车
    @Override
    public List<ShoppingCart> getCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        //获取token里面的用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> shippingCartList = shoppingCartMapper.select(shoppingCart);
        return shippingCartList;
    }
    //删除所有的购物车
    @Override
    public void deleteAll() {
        ShoppingCart shoppingCart = new ShoppingCart();
        //获取token里面的用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        shoppingCartMapper.deleteAll(shoppingCart);
    }

    //删除一个菜品
    //如果数量>1,就让数量-1
    //如果数量=1，就删除这条数据
    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //获取当前菜品数据
        List<ShoppingCart> shippingCartList = shoppingCartMapper.select(shoppingCart);
        ShoppingCart  shoppingCart1 = shippingCartList.get(0);
        //获取数量
        Integer number = shoppingCart1.getNumber();

        if(number>1){
           shoppingCart1.setNumber(number-1);
            shoppingCartMapper.updataNumber(shoppingCart1);
        }else {
            shoppingCartMapper.delete(shoppingCart);
        }
    }


}
